<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class ad extends core {
	private $ad_mod, $types, $positions;
	public function __construct() {
		$this->ad_mod = m("ad");
		$this->types = $this->ad_mod->types();
		$this->positions = $this->ad_mod->positions();
		parent::__construct();
	}

	//列表
	public function index() {
		$where = '';
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$ad_id = (isset($_GET['ad_id'])) ? trim($_GET['ad_id']) : '';
		$begin_time = (isset($_GET['begin_time'])) ? trim($_GET['begin_time']) : '';
		$end_time = (isset($_GET['end_time'])) ? trim($_GET['end_time']) : '';
		$ad_type = (isset($_GET['ad_type'])) ? trim($_GET['ad_type']) : '';
		$position = (isset($_GET['position'])) ? trim($_GET['position']) : '';

		if ($keyword) {
			$where .= "AND (name LIKE '%{$keyword}%' OR ad_content LIKE '%{$keyword}%')";
		}
		if (strlen($ad_id)) {
			$where .= "AND id='{$ad_id}'";
		}
		if (strlen($begin_time)) {
			$where .= " AND add_time>='".strtotime($begin_time)."'";
		}
		if (strlen($end_time)) {
			$where .= " AND add_time<='".strtotime($end_time)."'";
		}
		if ($ad_type) {
			$where .= "AND (ad_type='{$ad_type}')";
		}
		if ($position) {
			$where .= "AND (position='{$position}')";
		}
		//分页
		$this->setpages(array('ad_id'=>$ad_id, 'keyword'=>$keyword, 'begin_time'=>$begin_time, 'end_time'=>$end_time, 'ad_type'=>$ad_type, 'position'=>$position));
		$sql = "SELECT * FROM {$this->tbp}ad WHERE 1 {$where} ORDER BY id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['ad_type'] = $this->ad_mod->type_name($row['ad_type']);
				$rs[$key]['position'] = $this->ad_mod->position_name($row['position']);
			}
		}
		$rs = add_domain_deep($rs, array('pic'));
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('ad_id',$ad_id);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('begin_time',$begin_time);
		$this->smarty->assign('end_time',$end_time);
		$this->smarty->assign('ad_type',$ad_type);
		$this->smarty->assign('position',$position);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('types', $this->types);
		$this->smarty->assign('positions', $this->positions);

		$this->display();
	}

	//add
	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$sql = "SELECT name, id FROM {$this->tbp}shop ORDER BY id ASC";
			$shop = $this->db->get_results($sql);
			$this->smarty->assign('shop', $shop);
			$this->smarty->assign('types', $this->types);
			$this->smarty->assign('positions', $this->positions);
			$this->display();
		}
	}

	//编辑
	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			$ad_id = (isset($_GET['id']) && intval($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}ad WHERE id='{$ad_id}'";
			$ad = $this->db->get_row($sql);
			$ad->begin_time = date("Y-m-d H:i", $ad->begin_time);
			$ad->end_time = date("Y-m-d H:i", $ad->end_time);
			$ad = add_domain_deep($ad, array('pic'));
			$this->smarty->assign('row', $ad);
			$sql = "SELECT name, id FROM {$this->tbp}shop ORDER BY id ASC";
			$shop = $this->db->get_results($sql);
			$this->smarty->assign('shop', $shop);
			$this->smarty->assign('types', $this->types);
			$this->smarty->assign('positions', $this->positions);
			$this->display();
		}
	}

	//保存
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$ad_content = (isset($_POST['ad_content']) && trim($_POST['ad_content'])) ? trim($_POST['ad_content']) : '';
		$begin_time = (isset($_POST['begin_time']) && trim($_POST['begin_time'])) ? trim($_POST['begin_time']) : '';
		$end_time = (isset($_POST['end_time']) && trim($_POST['end_time'])) ? trim($_POST['end_time']) : '';
		$ad_type = (isset($_POST['ad_type']) && trim($_POST['ad_type'])) ? trim($_POST['ad_type']) : '';
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 999;
		$position = (isset($_POST['position']) && trim($_POST['position'])) ? trim($_POST['position']) : 'index';
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$channel = (isset($_POST['channel']) && intval($_POST['channel'])) ? intval($_POST['channel']) : 0;
		$shop_id = (isset($_POST['shop_id']) && intval($_POST['shop_id'])) ? intval($_POST['shop_id']) : 0;
		$offer = (isset($_POST['offer']) && intval($_POST['offer'])) ? intval($_POST['offer']) : 0; //发送优惠消息
		$pic = upload_one_file('ad', 'pic');
		if (!$pic) $pic = (isset($_POST['origin_pic']) && trim($_POST['origin_pic'])) ? trim($_POST['origin_pic']) : '';

		if ($begin_time) {
			$begin_time = strtotime($begin_time);
		}
		if ($end_time) {
			$end_time = strtotime($end_time);
		}

		if ($id) {
			$sql = "UPDATE {$this->tbp}ad SET name='{$name}', ad_content='{$ad_content}', ad_type='{$ad_type}',
				sort='{$sort}', position='{$position}', begin_time='{$begin_time}', end_time='{$end_time}',
				status='{$status}' ,channel='{$channel}',shop_id='{$shop_id}' WHERE id='{$id}' ";
			$this->db->query($sql);
			//更新图片
			if($pic){
				$sql = "UPDATE {$this->tbp}ad SET pic = '{$pic}' WHERE id = '{$id}'";
				$this->db->query($sql);
			}
		} else {
			$sql = "INSERT INTO {$this->tbp}ad(name, ad_content, ad_type, pic, sort, position, status, add_time, begin_time,
					end_time ,channel ,shop_id) VALUES('{$name}', '{$ad_content}', '{$ad_type}', '{$pic}', '{$sort}',
					'{$position}', '{$status}', '".time()."', '{$begin_time}', '{$end_time}' , '{$channel}', '{$shop_id}')";
			$this->db->query($sql);
			$ad_id = $this->db->insert_id;
		}
		//echo $sql;
		//exit;
		if ($offer) {
			$this->save_message($name, 0, 'offer');
		}

		header("Location:?app=ad");
	}

	//删除
	public function delete() {
		$ad_id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM {$this->tbp}ad WHERE id='{$ad_id}'";
		$this->db->query($sql);
		header("Location:?app=ad&act=index");
	}
}
